﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace PrintLabel.Tools
{
    public  class RollHelper
    {
       
        /// <summary>
        /// 获取一个缸卷号
        /// </summary>
        /// <param name="juanHaoTable"></param>
        /// <returns></returns>
        public static int CreatGangJuanhao(JuanHaoTable juanHaoTable)
        {
            try
            {
                List<JuanHaoTable> res = new List<JuanHaoTable>();
                //StockTable stock = dbContext.dbcontext.Queryable<StockTable>().First (x => x.ID == stockID);
                res = dbContext.dbcontext.Queryable<JuanHaoTable>().Where(x => x.state == 0 && x.SampleNum == juanHaoTable.SampleNum && x.Ckmc == juanHaoTable.Ckmc
                && x.OrderNum == juanHaoTable.OrderNum && x.GangHao == juanHaoTable.GangHao && x.Houzhengli == juanHaoTable.Houzhengli && x.kuanhao == juanHaoTable.kuanhao && x.yanse == juanHaoTable.yanse && x.Huahao == juanHaoTable.Huahao).ToList();
                int pihao = 1;
                while (true)
                {
                    var resnew = res.Where(x => Convert.ToInt32(x.PiHao) == pihao).ToList();
                    if (resnew.Count == 0)
                    {
                        return pihao;
                    }
                    pihao++;
                }
            }
            catch
            {
                return Convert.ToInt32(1);
            }
        }
        public static void CreateStock(JuanHaoTable roll)
        {

                var stock = dbContext.dbcontext.Queryable<StockTable>().First(x => x.CKMC == roll.Ckmc && x.orderNum == roll.OrderNum && x.BH == roll.SampleNum && x.kuanhao == roll.kuanhao && x.YS == roll.yanse && x.GH == roll.GangHao);
                if (stock != null)
                {
                    stock.MS += roll.biaoqianmishu;
                    stock.JS++;
                    dbContext.dbcontext.Updateable(stock).ExecuteCommand();
                }
                else
                {
                    stock = new StockTable()
                    {
                        MS = roll.biaoqianmishu,
                        JS = 1,
                        BH = roll.SampleNum,
                        PM = roll.SampleName,
                        kuanhao = roll.kuanhao,
                        YS = roll.yanse,
                        GH = roll.GangHao,
                        CustomerPingMing = roll.CustomerPingMing,
                        biaoqianmishu = roll.biaoqianmishu,
                        ContractNum = roll.Hetonghao,
                        GG = roll.guige,
                        Huahao = roll.Huahao,
                        KZ = roll.Kezhong,
                        MF = roll.Menfu,
                        orderNum = roll.OrderNum,
                        CKMC = roll.Ckmc,
                        CustomerColorNum = roll.CustomerColorNum,
                        CustomName=roll.CustomerName ,
                        CF=roll.Composition,
                        Density=roll.Density,
                        EnglishName=roll.EnglishName,
                        SecondaryEncoding=roll.SecondaryEncoding,
                    };
                    stock.MapperFrom(roll);
                    dbContext.dbcontext.Insertable(stock).ExecuteCommand();
            }
        }
      
    }
}
